<?php


namespace app\admin\controller;


use app\common\model\Address;
use app\common\model\ApplyReg;
use app\common\model\Notice;
use app\common\model\Order;
use think\Request;
use think\Db;

class Shops extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('shopapply/applylist')
     * 申请注册商铺列表
     */
    public function applylist()
    {
//        var_dump(123);die;
//        $suborgModel = new \app\common\model\Shops();
//
//
//        // 获取搜索条件
        $get['shop_name'] = input('get.shop_name');
        $where = null;
        if($get['shop_name']){
            $where[] = ['shopname','like',"%".$get['shop_name']."%"];
        }
        $model = new ApplyReg();
        $rs_data = $model->get_list($get, 'a.status asc','a.*,b.name as pro_name, c.name as city_name, d.name as area_name, f.name as street_name',$this->limit);

//        $rs_data = $model->where($where)->paginate($this->limit);

//        var_dump($rs_data->toArray());die;
        $tvar = array(
            "title" => "商铺注册申请列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
//            "isdisplay" => $isdisplay,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('shops/applylist');
    }


    /**
     * @route('shopapply/boxcheck')
     * 申请审核
     */
    public function BoxCheck()
    {
        $model = new ApplyReg();
        $get['applyreg_id'] = input('get.applyreg_id');
//        var_dump($get['applyreg_id']);die;
        if(request()->isAjax()){
            $param = request()->param();
            if(empty($param['applyreg_id']) || empty($param['status'])){
                $this->error('必填项不能为空');
            }
            $data = [
                'status' => $param['status'],
                'reasons' => $param['reasons'],
            ];
//            var_dump($param);die;
            $rs = $model->edit($data,$param['applyreg_id'],true);
            if($rs === false){
                $this->error($model->getError());
            }
            $this->success('操作成功','/shopapply/applylist');
        }
        $checkdetail = $model->where('applyreg_id','=',$get['applyreg_id'])->find();
//        var_dump($checkdetail);
        if(!$checkdetail){
            $this->error('不存在的审核','/shopapply/applylist');
        }
        $tvar = array(
            "title" => "添加账号",
            "time"  => time(),
            "applyreg_id" => $get['applyreg_id'],
        );
        $this->assign("data", $tvar);
        return $this->fetch('shops/boxcheck');
    }

    /**
     * @route('shops/shopslist')
     * 商铺管理列表
     */
    public function shopslist()
    {
//        // 获取搜索条件
        $get['shop_name'] = input('get.shop_name');
        $where = null;
        if($get['shop_name']){
            $where[] = ['shop_name','like',"%".$get['shop_name']."%"];
        }
//        var_dump(345);die;
        $model = new \app\common\model\Shops();
        $rs_data = $model->where($where)->paginate($this->limit);
        $tvar = array(
            "title" => "商铺管理列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
        );
//        var_dump(345);die;
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('shops/shopslist');
    }

    /**
     * @route('shops/updatestate')
     * 商铺状态修改
     */
    public function UpdateState()
    {
        $shopid = input('shopid');
        $model = new \app\common\model\Shops();
        $adminModel = new \app\common\model\Admin();
        $shop = $model->where('shop_id','=',$shopid)->find();
        if(empty($shop)){
            $this->error('不存在该商铺');
        }
        $data = $shop->toArray();
        if($data['state'] == 1){
            $param['state'] = 2;
        } else {
            $param['state'] = 1;
        }
        $admin = $adminModel->where('shop_id','=',$shopid)->find();
        if(empty($admin)){
            $this->error('不存在商铺对应管理员');
        }
        $admindata = $admin->toArray();
        if($admindata['status'] == 1){
            $adminparam['status'] = 2;
        } else {
            $adminparam['status'] = 1;
        }
        $model->startTrans();
        $rs = $model->edit($param,$data['shop_id']);
        if($rs === false){
            $model->rollback();
            $this->error("操作失败");
        }
        $rs2 = $adminModel->edit($adminparam,$admindata['user_id']);
        if($rs2 === false){
            $model->rollback();
            $this->error("操作失败");
        }
        $model->commit();
        $this->success('操作成功');
    }




    /**
     * @route('shops/reportlist')
     * 商铺报表
     */
    public function ReportList()
    {
        $orderModel = new Order();

        $get['shop_name'] = input('get.shop_name');
        $get['shop_type'] = input('get.shop_type');
        $get['shop_id'] = $this->_shopid;

        $rs_data = $orderModel->ShopsReportFormList($get,$this->limit);
//        $rs_data = $orderModel->ShopsReportFormList($get,$this->limit)->toArray();
//        echo "<pre>";
//        var_dump($rs_data['data']);die;

        $tvar = array(
            "title" => "商铺报表",
            "time"  => time(),
            "data" => $rs_data,
            "get"  => $get,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('shops/reportlist');
    }



    /**
     * @route('shops/noticelist')
     * 消息通知
     */
    public function NoticeList()
    {

        $NoticeModel = new Notice();

//        // 获取搜索条件
        $cond['shop_name'] = input('get.shop_name');

        $cond['shop_id'] = $this->_shopid;
        $isdisplay = true;
        if($cond['shop_id'] > 0){
            $isdisplay = false;
        }
        $rs_data = $NoticeModel->get_list_page($cond,'shop_id asc, id desc','a.*,c.shop_name,u.nick_name',$this->limit);


        $tvar = array(
            "title" => "消息通知列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $cond,
            "isdisplay" => $isdisplay,
        );
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('shops/notice/noticelist');
    }


    /**
     * @route('shops/boxenotice')
     * 消息通知
     */
    public function BoxeNotice()
    {
        $NoticeModel = new Notice();
        $AddressModel = new Address();

        if(request()->isAjax()){
           $params = input();


            $validate = new \app\admin\validate\Notice();

            $res = $validate->scene('EditNotice')->check($params);
            if(!$res){
                $this->error($validate->getError());
            }

            $userId = 0;
           if($params['type'] == 1){
               // 通知个人
               if(empty($params['phone'])){
                   $this->error('通知类型为个人时必须填写通知人手机号码');
               }
               $userId = $AddressModel->getOne(['phone' => $params['phone']],'user_id')['user_id'];
               if(!$userId){
                   $this->error('手机号码查无此用户');
               }
           }

            $param['shop_id'] = $this->_shopid;
            $param['admin_userid'] = $this->_userid;
            $param['admin_username'] = $this->_user_name;
            $param['user_id'] = $userId;
            $param['title'] = $params['title'];
            $param['summary'] = $params['summary'];
            $param['content'] = $params['content'];


            $rs = $NoticeModel->edit($param, 0);
            if($rs === false){
                $this->error($NoticeModel->getError());
            }
            $this->success('操作成功','/shops/noticelist');
        }


        $tvar = [
            'time' => time(),
            'title' => '添加消息他哦内阁制'
        ];
        $this->assign("data", $tvar);
        return $this->fetch('shops/notice/boxenotice');
    }



    /**
     * @route('shops/delnotice')
     * 删除消息通知
     */
    public function DelNotice()
    {
        $id = input('id');
        if(!$id){
            $this->result('',-1, '请提交正确的ID');
        }

        $rs = Notice::destroy($id);
        if($rs === false){
            $this->error('删除失败');
        }
        $this->success('删除成功');

    }



}